package com.jcfy.project.module.service.impl;

import com.jcfy.common.utils.DateUtils;
import com.jcfy.common.utils.StringUtils;
import com.jcfy.project.module.domain.TDictType;
import com.jcfy.project.module.mapper.TDictDataMapper;
import com.jcfy.project.module.mapper.TDictTypeMapper;
import com.jcfy.project.module.service.ITDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 字典类型Service业务层处理
 *
 * @author hushuai
 * @date 2020-07-06
 */
@Service
public class TDictTypeServiceImpl implements ITDictTypeService {
    @Autowired
    private TDictTypeMapper tDictTypeMapper;
    @Autowired
    private TDictDataMapper tDictDataMapper;

    /**
     * 查询字典类型
     *
     * @param dictId 字典类型ID
     * @return 字典类型
     */
    @Override
    public TDictType selectTDictTypeById(Long dictId) {
        return tDictTypeMapper.selectTDictTypeById(dictId);
    }

    /**
     * 查询字典类型列表
     *
     * @param tDictType 字典类型
     * @return 字典类型
     */
    @Override
    public List<TDictType> selectTDictTypeList(TDictType tDictType) {
        return tDictTypeMapper.selectTDictTypeList(tDictType);
    }

    /**
     * 新增字典类型
     *
     * @param tDictType 字典类型
     * @return 结果
     */
    @Override
    public int insertTDictType(TDictType tDictType) {
        tDictType.setCreateTime(DateUtils.getNowDate());
        return tDictTypeMapper.insertTDictType(tDictType);
    }

    /**
     * 修改字典类型
     *
     * @param tDictType 字典类型
     * @return 结果
     */
    @Override
    @Transactional
    public int updateTDictType(TDictType tDictType) {
        TDictType oldDict = tDictTypeMapper.selectTDictTypeById(tDictType.getDictId());
        tDictDataMapper.updateTDictDataType(oldDict.getDictType(), tDictType.getDictType());
        tDictType.setUpdateTime(DateUtils.getNowDate());
        return tDictTypeMapper.updateTDictType(tDictType);
    }

    /**
     * 批量删除字典类型
     *
     * @param dictIds 需要删除的字典类型ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTDictTypeByIds(Long[] dictIds) {
        for (int i = 0; i < dictIds.length; i++) {
            TDictType tDictType = tDictTypeMapper.selectTDictTypeById(dictIds[i]);
            tDictDataMapper.deleteTDictDataByType(tDictType.getDictType());
        }
        return tDictTypeMapper.deleteTDictTypeByIds(dictIds);
    }

    /**
     * 删除字典类型信息
     *
     * @param dictId 字典类型ID
     * @return 结果
     */
    @Override
    public int deleteTDictTypeById(Long dictId) {
        return tDictTypeMapper.deleteTDictTypeById(dictId);
    }

    @Override
    public boolean checkTDictTypeUnique(TDictType dict) {
        Long dictId = StringUtils.isNull(dict.getDictId()) ? -1L : dict.getDictId();
        TDictType tDictType = tDictTypeMapper.checkTDictTypeUnique(dict.getDictType());
        if (StringUtils.isNotNull(tDictType) && tDictType.getDictId().longValue() != dictId.longValue()) {
            return false;
        }
        return true;
    }
}
